package cn.kgc.config;

import cn.kgc.vo.PayVo;
import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@Data
@Slf4j
public class AlipayTemplate {
    // 商户appid
    public static String APPID = "2021000122602157";
    // 私钥 pkcs8格式的
    public static String RSA_PRIVATE_KEY = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCR4Q03FjIO/JD692STgoIuy4oteGvDZjyXcpJu+5tQm27xgC4DQDlzUM0/WBHuSoh8Q2yQ7JyFxIwBV0ihWLA5SN4gusPmEcn7iZuCeMmHKlo5hd2jEkLowX5auD5Gb4z4sVzfSO7moIZ2AKzb18AVNL5OB+PizI3JLWYtjk/nCErycnFCFKllIcPLx6LDCl/Q4+wUNwhiR6dgXgSjm2GjtWM83v4fEvJ68axWwtEpTC3dg4xlhVQFFDbi9HIqEFfSJWL5zCI+YtB7N0OST4ncGLn2PjLsnbIB29HspgKJ5aSNEfyRq+EvDf0o/gcn+NgReYY8kS2HGGHP09/FIu7dAgMBAAECggEAeq9oGtUa4om9MKQRPRG8hLh/jEBDeUEpYkFkL9yBQeiHBKpFiExiRL5G76gU5HBgmwlla/jipomnKZWLqs5Y3sSOs73+9SYu1dJkK/WDn56Z5NjVjsjT5sacipV7Ime8ioc30T2YVfPZQbLNrdKkY/FIv31ZRau87t20mkmen3CFvVKBkhgY8gSRBxgolpqJOHW3gt8/jhVb24sB1a87nrrE6YrSjNB/Pb8g9qfUi7by+UXi+g0cW+wrgDIM3gJmiwW0C00QxZZEFuzv5UwcwL58yICGkN2s9aAnKDQcsHEfsQyVAfumGSe4Nzc6/gHblykCiYZy6CxMOlRu9buzwQKBgQD4IExoqpW5qov5ouWgLPb49h7q6UkdCYztUrJ3sUytca2oLQpCaDSSgFNYDmdEIcpZT7sNs5VUyyVSzDRY+k1mn1Kd2G659evcDaeCES3m2uqGVJiNraiQiW6Uh7udwDRkat7q0a2i3YonFbA/f7UwsoVNh2S/Pr+gGKxLToP0jQKBgQCWgiEPQ2yfUeMpCbA8FWnRC6oK275cUTb4yhKB0b9+VzkN154q1JEA+wL1DdhXwwO+NkOwV5ktT33CR1XuKtsA7ZRazy1Q75pOjVqp3duBt4r3ldMvpVWJRctxpSB410iWRwMHgE/NqbkAgmXCZM5MhWxHOXVU2aUSQqPerM/XkQKBgEIZwKFFiHuczotjdQ7Vy7mJP7VPDpY4vB1hIYA3cHjoRt39mbUVM6QvoYRc2e/RnKlsTyWYSNj9AWVsVym5DLOhEA5S4TSTKhFGZfCFiRfuC/pbQQTFVZmjsxzIiBe/Y3W47sfZsgsoP6yn33sbF3bwqAh+YD1+hugsioHJWeflAoGBAIP5qXmT2ty9Vl620yVsdXRMMRgCZiLXSEfSEXZqcOsdGrdrmZLfACOG96ngMROLum2BcctglL2plZHVMaF9TLq8mTkjsA3nfDxjMNwA4akWQ7IkW6HM7XtIi0Ok0xuDU3AK5roeJlwl9kZfEVHrSQbKfA7ccGkty9R+QOlSSZIxAoGBANFS5iUE+FR+ZNBgp4SYWSQKKRU4aw/hVprkesDns70nBVKT47Se27jEIRhfG8tZW2IIqaxezTl6MNKLcdW+b2AiOcl520IA4L5hvDgvuaz2ygGf8LLi6KAEbFBZS1wfhkJOG6T4kevdWNXz16C1luHBN4jCuOrbHCx5ZFyx9V9O";

    // 服务器异步通知页面路径 需http://或者https://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    public static String notify_url="https://6742t58s11.zicp.fun/order/notifyUrl";

    // 页面跳转同步通知页面路径 需http://或者https://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问 商户可以自定义同步跳转地址
    public static String return_url="https://6742t58s11.zicp.fun/order/returnUrl";
    // 编码
    public static String CHARSET = "UTF-8";
    // 返回格式
    public static String FORMAT = "json";
    // 请求网关地址
    public static String URL = "https://openapi.alipaydev.com/gateway.do";
    // 支付宝公钥
    public static String ALIPAY_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArQAzjVH7N7ykIEyYpgGFS8QrIw7VdgCW7n5At4BBk+gFBpaXf6kOC8pL2wSGeyBZm+QwRXVysbaHbhK3wVYdJ5/yIQn19K/vDFjCyuDjTs1EswzDgrNnVisEW/EDk3culnMuOwLM6Y9n9SaYeFbOHfyeZYi0E+spXPK4dFR+sDG9RfLY3UOPMu2LFroxh6+D6faqB8thkenkfC+B7bt3CfNUqgAsAGLh7nCbVGoWmgfxixkgq+woDV92zQGnVPjgONQb8kZZDMbRKV1Kzs8QvYGexdQteGEEJXlxWyfAUFQ1i4hl/jcmhTRyrurluA5A3mS1auWsw9//B6ukcClAlwIDAQAB";
    // RSA2
    public static String SIGNTYPE = "RSA2";


    public String pay(PayVo payVo)throws AlipayApiException{
        //创建alipay支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(URL,
                APPID,RSA_PRIVATE_KEY,FORMAT,CHARSET,ALIPAY_PUBLIC_KEY,SIGNTYPE);

        //创建支付请求
        AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
        request.setReturnUrl(return_url);
        request.setNotifyUrl(notify_url);
       request.setBizContent(JSON.toJSONString(payVo));
       //支付客户端发送请求，获得支付宝返回的接口
       String result=alipayClient.pageExecute(request).getBody();
       return result;
    }
}